import datetime
from zoneinfo import ZoneInfo

import requests
import json
from .uu_data_model import *
import time
import yaml
from pathlib import Path


def crawl_goods(goods_list, interval):
    url = "https://api.youpin898.com/api/homepage/es/commodity/GetCsGoPagedList"
    res = []

    for goods in goods_list:
        payload = json.dumps({
            "hasSold": "true",
            "haveBuZhangType": 0,
            "listSortType": "1",
            "listType": 10,
            "pageIndex": 1,
            "pageSize": 1,
            "sortType": "1",
            "status": "20",
            "stickersIsSort": False,
            "templateId": f"{goods}",
            "userId": ""
        })
        headers = {
            'Content-Type': 'application/json'
        }
        response = requests.request("POST", url, headers=headers, data=payload).json()

        market_model = prase_json(response)

        if market_model is not None:
            res.append(market_model)

        time.sleep(interval)

    return res


def prase_json(response):
    if response["Code"] != 0 or response["Data"] is None or len(response["Data"]) == 0 or response["Data"]["TemplateInfo"] is None:
        return None

    name = response["Data"]["TemplateInfo"]["CommodityName"] \
        if response["Data"]["TemplateInfo"]["CommodityName"] is not None else ''

    template_id = response["Data"]["TemplateInfo"]["Id"] \
        if response["Data"]["TemplateInfo"]["Id"] is not None else ''

    type_ = response["Data"]["TemplateInfo"]["typeHashName"] \
        if response["Data"]["TemplateInfo"]["typeHashName"] is not None else ''

    exterior = response["Data"]["TemplateInfo"]["Exterior"] \
        if response["Data"]["TemplateInfo"]["Exterior"] is not None else ''

    min_price = float(response["Data"]["TemplateInfo"]["MinPrice"]) \
        if response["Data"]["TemplateInfo"]["MinPrice"] is not None else 0

    on_sale_count = response["Data"]["TemplateInfo"]["OnSaleCount"] \
        if response["Data"]["TemplateInfo"]["OnSaleCount"] is not None else 0

    on_lease_count = response["Data"]["TemplateInfo"]["OnLeaseCount"] \
        if response["Data"]["TemplateInfo"]["OnLeaseCount"] is not None else 0

    lease_unit_price = float(response["Data"]["TemplateInfo"]["LeaseUnitPrice"]) \
        if response["Data"]["TemplateInfo"]["LeaseUnitPrice"] is not None else 0

    long_lease_unit_price = float(response["Data"]["TemplateInfo"]["LongLeaseUnitPrice"]) \
        if response["Data"]["TemplateInfo"]["LongLeaseUnitPrice"] is not None else 0

    lease_deposit = float(response["Data"]["TemplateInfo"]["LeaseDeposit"]) \
        if response["Data"]["TemplateInfo"]["LeaseDeposit"] is not None else 0

    return MarketModel(
        datetime.datetime.now(ZoneInfo("Asia/Shanghai")).strftime("%Y-%m-%d %H:%M:%S"),
        name,
        str(template_id),
        type_,
        exterior,
        min_price,
        on_sale_count,
        on_lease_count,
        lease_unit_price,
        long_lease_unit_price,
        lease_deposit
    )

def calculate(obj1, obj2):
    return {
        'date': obj1['date'],
        'template_id': obj1['template_id'],
        'name': obj1['name'],
        'min_price': obj1['min_price'],
        'on_sale': obj1['on_sale_count'],
        'last_min_price': obj2['min_price'],
        'last_on_sale': obj2['on_sale_count'],
        'delta_price': obj1['min_price'] - obj2['min_price'],
        'delta_price_rate': (obj1['min_price'] - obj2['min_price']) / obj2['min_price'],
        'delta_sale_count': obj1['on_sale_count'] - obj2['on_sale_count'],
        'delta_sale_count_rate': (obj1['on_sale_count'] - obj2['on_sale_count']) / obj2['on_sale_count']
    }

def load_config_from_yaml(config_file):
    """从 YAML 文件加载配置"""
    try:
        with Path(config_file).open('r', encoding='utf-8') as f:
            return yaml.safe_load(f)
    except Exception as e:
        print(f"加载配置文件时出错: {e}")
        return {}
